package com.zmn.oms.api.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.entrust.SyncEntrustOrderStatusDTO;
import com.zmn.oms.model.dto.open.v2.PushOrderDTO;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.zmn.facade.interfaces.open.EntrustOrderNotifyFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 类描述：委托单变化状态推送
 *
 * @author lhl
 * @date 2020/01/10 18:20
 */
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
public class EntrustOrderNewListener {

    private static final Logger logger = LoggerFactory.getLogger(EntrustOrderNewListener.class);

    /**
     * 新单
     *
     * @param message
     */
    @AliyunMQHandler(tags = OmsMqTagConsts.ZMN_TOPIC_OMS_ENTRUST_ORDER_SYNC_NEW_API)
    public void orderNew(String message) {
        try {
            logger.info("#mq#entrustOrder#new#recive message:[{}]", message);
            OrderWorkLogChange orderWorkLogChange = JSON.parseObject(message, OrderWorkLogChange.class);
            EntrustOrderNotifyFacade entrustOrderNotifyFacade
                    = OrderChangeNotifyFactory.getNewEntrustOrderNotifyInstance(orderWorkLogChange);
            if (entrustOrderNotifyFacade != null) {
                PushOrderDTO pushOrderDTO = new PushOrderDTO();
                pushOrderDTO.setOrderId(String.valueOf(orderWorkLogChange.getOrderId()));
                pushOrderDTO.setOptTime(orderWorkLogChange.getOpTime().getTime() / 1000);

                ResultDTO<SyncEntrustOrderStatusDTO> resultDTO = entrustOrderNotifyFacade.pushNewOrder(pushOrderDTO);

                logger.info("#mq#entrustOrder#syncnew#result Result:[{}]", JSON.toJSONString(resultDTO));

            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}
